﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditPluginTemplateLiterals : AdminPage, IEditPluginTemplateLiteralsView
    {
        private EditPluginTemplateLiteralsPresenter _presenter;
        private PluginTemplateLiteral _currentLiteral;


        public override string HelpPage
        {
            get { return "dialogs/edit-plugin-template-literals.aspx"; }
        }

        #region IEditPluginTemplateLiteralsView Members

        public int TemplateID
        {
            get
            {
                if (ViewState["TemplateID"] == null)
                    ViewState["TemplateID"] = QueryString.GetInt32ID("id");
                return (int) ViewState["TemplateID"];
            }
        }


        public int PluginID
        {
            get
            {
                if (ViewState["PluginID"] == null)
                    ViewState["PluginID"] = QueryString.GetInt32ID("pluginid");
                return (int) ViewState["PluginID"];
            }
            set { ViewState["PluginID"] = value; }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditPluginTemplateLiterals.xml"; }
        }


        public void SetLiterals(IList<PluginTemplateLiteral> literals)
        {
            rpLiterals.DataSource = literals;
            rpLiterals.DataBind();
        }


        public IList<PluginTemplateLiteral> GetTranslations()
        {
            IList<PluginTemplateLiteral> literals = new List<PluginTemplateLiteral>();
            foreach (RepeaterItem riLiteral in rpLiterals.Items)
            {
                var literal = new PluginTemplateLiteral();
                var lblName = (Label) riLiteral.FindControl("lblName");
                literal.ID = int.Parse(lblName.Attributes["literalid"]);
                var rpTranslations = (Repeater) riLiteral.FindControl("rpTranslations");
                foreach (RepeaterItem riTranslation in rpTranslations.Items)
                {
                    var txtHtml = (TextBox) riTranslation.FindControl("txtHtml");
                    literal.SetHtml(int.Parse(txtHtml.Attributes["localeid"]), txtHtml.Text);
                }
                literals.Add(literal);
            }
            return literals;
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditPluginTemplateLiteralsPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
        }


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            hlCancel.NavigateUrl = "Admin/PluginTemplates.aspx?id=" + PluginID;
        }


        protected void rpLiterals_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var literal = (PluginTemplateLiteral) e.Item.DataItem;
            var lblName = (Label) e.Item.FindControl("lblName");
            var rpTranslations = (Repeater) e.Item.FindControl("rpTranslations");
            lblName.Text = literal.ControlID;
            lblName.Attributes.Add("literalid", literal.ID.ToString());
            _currentLiteral = literal;
            rpTranslations.DataSource = _presenter.GetLocales();
            rpTranslations.DataBind();
        }


        protected void rpTranslations_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var locale = (Locale) e.Item.DataItem;
            var litLocale = (Literal) e.Item.FindControl("litLocale");
            var txtHtml = (TextBox) e.Item.FindControl("txtHtml");
            litLocale.Text = locale.GetTranslation(Localization).Name;
            txtHtml.Text = _currentLiteral.GetHtml(locale.ID);
            txtHtml.Attributes.Add("localeid", locale.ID.ToString());
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
        }


        protected void lbSaveAndReturn_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (_presenter.IsValid)
                Response.Redirect("PluginTemplates.aspx?id=" + PluginID);
        }
    }
}